
/**
 * HXS - Haxe Signals Library
 * @author Tony Polinelli
 */

 
package hxsTest;

import flash.display.DisplayObject;
import flash.display.LoaderInfo;
import flash.display.MovieClip;
import flash.events.TimerEvent;
import flash.utils.Timer;
import hxs.events.flash.display.InteractiveObjectEvents;
import hxs.extras.FlashEventsStore;
import hxs.extras.FlashSignal;
import hxs.core.Info;
import hxs.events.flash.display.DisplayObjectEvents;
import hxs.Signal;
import hxs.Signal1;
import hxs.Signal2;
import hxs.Signal3;
import hxs.Signal4;
import hxs.Signal5;
import flash.net.URLLoader;
import flash.net.URLRequest;

import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.Lib;
import flash.text.TextField;


using hxs.Shortcuts;

class Flash extends Sprite
{	
	/*
	
	Comment / uncomment the examples to view each example
	
	*/
	public var onClick:Signal1<Int>;
	
	public function new()
	{
		super();
		
		Lib.current.addChild(this);

		testAS3();
		testShortcuts();
		testExtendedEvent();
	}
	
	/*
	
	An AS3 event expects to be bound to a listener which expects 1 argument (the native flash event). it is a simple way to use Signal style events with Native Flash events. 
	
	*/
	public function testAS3()
	{
		// SPRITE
		var box = new Box(0xffff00);
		box.x = 300;
		addChild(box);
		
		var onClick = new FlashSignal(box, MouseEvent.CLICK);
		
		onClick.add(function(e){
			trace("clicked");
		});	
		
	}
	
	/*
	
	SHORTCUTS
	
	Signals can be injected to InteractiveObjects via the 'using' feature. This makes for very concise code.
	All objects which dispatch events have shortcuts attached.
	*/
	public function testShortcuts()
	{	
		// Timer Example
		var timer = new Timer(1, 5);
		timer.events().timer.add(function(e)
		{
			trace("Timer - "+timer.currentCount);
		});
		timer.events().timerComplete.add(function(e)
		{
			trace("Timer - complete");
		});
		
		// Interactive Object Example
		var s = new Sprite();
		s.graphics.beginFill(0xff0000);
		s.graphics.drawCircle(0, 0, 50);
		s.events().click.addOnce(function(e){
			trace("sprite - you can only click me once");
		});
		s.events().releaseOutside.add(function(e) {
			trace("OUTSIDE!");
		});
		
		s.x = 550;
		s.y = 50;
		addChild(s);
		
		// URLLoader Example
		var urlLoader = new URLLoader();
		urlLoader.events().progress.add(function(e) {
			
			trace("URLLoader - Loaded : " + (Math.floor(e.bytesLoaded/100)/10) + "kb Loaded");
		});
		urlLoader.events().complete.add(function(e) {
			trace("URLLoader - Complete");
		});
		urlLoader.load(new URLRequest("http://www.google.com"));
	}
		
	public function testExtendedEvent()
	{
		var box = new Box(0x0000ff);
		box.x = 400;
		box.events.added.add(function(e)
		{
			trace("Added");
		});
		
		box.events.click.add(function(e){
			trace("box click");
		});
		
		box.events.rollOver.add(function(e){
			box.alpha = .5;
			trace("box rollOver");
		});
		
		box.events.rollOut.add(function(e){
			box.alpha = 1;
			trace("box rollOut");
		});
		
		box.events.customEvent1.add(function(v){
			trace("Custom Event - called, external listerner");
			trace(v);
		});
		
		
		addChild(box);
	}
	
}

class Box extends flash.display.Sprite
{
	public var events:ExtendedEvents;
	
	public function new(color:Int)
	{
		super();
		
		graphics.beginFill(color, 1);
		graphics.drawRect(0, 0, 100, 100);
		
		events = new ExtendedEvents(this);
		events.click.add(handleClick);
	}
	
	public function handleClick(e)
	{
		// Timer events
		var timer = new Timer(1, 10);
		
		timer.events().timer.add(function(e){
			trace("Box Timer - "+timer.currentCount);
		});
		
		timer.events().timerComplete.add(function(e){
			trace("Box Timer - compltete - call custom event");
			events.customEvent1.dispatch(23.2);
		});
		
		timer.start();
	}
}

import hxs.events.flash.display.InteractiveObjectEvents;

class ExtendedEvents extends InteractiveObjectEvents
{
	public var customEvent1:Signal1<Float>;
	
	override public function new(e)
	{
		super(e);

		customEvent1 = new Signal1();
	}
}